<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            工单上料库转
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">操作类型 </div>
                    <div class="aui-list-item-input">
                        <label><input class="aui-radio" type="radio" id="inType" value="IN" name="type" checked>
                            上料</label>
                        <label><input class="aui-radio" type="radio" id="outType" value="OUT" name="type"> 退料</label>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">工单</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择工单" id="workOrderCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWO()"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">目标库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 目标库位" id="relWorkCellCode" value=""
                            onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('2')"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();"
                            onfocus="focusFn(this)">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料名称</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="produName" disabled="true">
                    </div>
                </div>
            </li>

            <li class="aui-list-item" style="display:none" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tm">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码唯一号ID</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tmId">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="mrlCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">转移数量</div>
                    <div class="aui-list-item-input">
                        <input type="number" id="qty">
                    </div>
                </div>
            </li>

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">来源库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 来源库位" id="workCellCode" value="" readonly="readonly" />
                    </div>
                    <!-- <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell('1')"></i> -->
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="margin-left:10%;width:30%;" onclick="doSearch()">查询</div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:10%;" onclick="save()">保存</div>
        <div class="aaui-btn-info aui-btn-block" id="infoDiv"
            style="margin-top:2%;margin-bottom: 10px;color: black !important;background-color: #c2c2c2 !important;height: 175px;padding: 0.2rem 0.2rem;font-size: 0.8rem;">
            <p id="resultInfo" style="color: #000000;font-size: 0.8rem;"></p>
            <p id="boxnumtital" style="color: #000000;font-size: 0.8rem;white-space:nowrap;"></p>
            <p id="mrlCodetital" style="color: #000000;font-size: 0.8rem;white-space:nowrap;"></p>
            <p id="lotCodetital" style="color: #000000;font-size: 0.8rem;white-space:nowrap;"></p>
            <p id="qtytital" style="color: #000000;font-size: 0.8rem;white-space:nowrap;"></p>
        </div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:20%;" id="tbodyHtml">

        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCellGid = ""; //来源库位GID
    var workCellCode = ""; //来源库位编码
    var workCellName = ""; //来源库位名称
    var relWorkCellGid = ""; //目标库位GID
    var relWorkCellCode = ""; //目标库位编码
    var relWorkCellName = ""; //目标库位名称
    var workCenterGid = "";//仓库GID
    var arrSave = [];
    var boxnumE = "";
    //初始化查询仓库下拉框
    apiready = function () {
        $("#workOrderCode").focus();
        idSelector = "workOrderCode";

        //来源库位默认
        document.getElementById('inType').addEventListener('change', function () {
            // 处理radio点击事件的代码
            workCellGid = "";
            workCellCode = "";
            workCellName = "";
            relWorkCellGid = "";
            relWorkCellCode = "";
            relWorkCellName = "";
            workCenterGid = "";
            $("#workOrderCode").val("");
            $("#produName").val("");
            $("#boxnum").val("");
            boxnumE = "";
            $("#tm").val("");
            $("#tmId").val("");
            $("#mrlCode").val("");
            $("#lotCode").val("");
            $("#qty").val("");
            $("#relWorkCellCode").val("");
            $("#workCellCode").val("");
            $api.html($api.byId("tbodyHtml"), "");

            $("#workOrderCode").focus();
            idSelector = "workOrderCode";
        });

        document.getElementById('outType').addEventListener('change', function () {
            // 处理radio点击事件的代码
            workCellGid = "";
            workCellCode = "";
            workCellName = "";
            relWorkCellGid = "";
            relWorkCellCode = "";
            relWorkCellName = "";
            workCenterGid = "";
            $("#workOrderCode").val("");
            $("#produName").val("");
            $("#boxnum").val("");
            boxnumE = "";
            $("#tm").val("");
            $("#tmId").val("");
            $("#mrlCode").val("");
            $("#lotCode").val("");
            $("#qty").val("");
            $("#relWorkCellCode").val("");
            $("#workCellCode").val("");
            $api.html($api.byId("tbodyHtml"), "");

            $("#workOrderCode").focus();
            idSelector = "workOrderCode";
        });
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        if (el.id == "workOrderCode") {
            $("#workOrderCode").select();
        }
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：来源库位
            if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询来源库位
                getWorkCell(ret, idSelector, "1");

                //24：目标库位
            } else if (idSelector == 'relWorkCellCode') {

                $("#relWorkCellCode").val(ret);
                //查询目标库位
                getWorkCell(ret, idSelector, "2");

                //5：扫描条码
            } else if (idSelector == 'boxnum') {

                tmSplit(ret);

                //工单号
            } else if (idSelector == 'workOrderCode') {

                $("#workOrderCode").val(ret);
                //值改变
                onChange();
            }
        });
    }

    //工单号值改变
    function onChange() {

        $("#tm").val("");
        $("#tmId").val("");
        $("#boxnum").val("");
        boxnumE = "";
        $("#mrlCode").val("");
        $("#lotCode").val("");
        $("#qty").val("");
        //1：查询表格数据
        doSearch();
        //2:查询源库位/或目标库位（根据操作类型）
        doSearchWorkCell();
    }

    //7:库位扫描校验
    function getWorkCell(code, inputId, flag) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCellCode: code, //库位编码
                appVersion: api.appVersion,
                flag: "out_kj",
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = myobj.list;

                    //源库位
                    if (flag == "1") {

                        workCellGid = list[0].workCellGid;
                        workCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                        workCellName = list[0].workCellName;
                        document.getElementById(inputId).value = workCellCode;

                        //目标库位
                    } else if (flag == "2") {

                        relWorkCellGid = list[0].workCellGid;
                        relWorkCellCode = list[0].workCellCode + ":" + list[0].workCellName;
                        relWorkCellName = list[0].workCellName;
                        document.getElementById(inputId).value = relWorkCellCode;
                    }
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    saveAuto()
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#" + inputId).focus();
                    $("#" + inputId).select();
                }
            }
        });
    }

    //8：选择库位
    function goSelectWorkCell(workCellFlag) {
        //操作类型
        var type = $("input[name='type']:checked").val();
        //上料
        if (type == "IN") {
            if (workCellFlag == '1') {
                //来源库位
                getworkCellLY(workCellFlag)
            }
            if (workCellFlag == '2') {
                //目标库位
                getworkCellMB(workCellFlag)
            }

        }
        //退料
        if (type == "OUT") {
            if (workCellFlag == '1') {
                //来源库位
                getworkCellMB(workCellFlag)
            }
            if (workCellFlag == '2') {
                //目标库位
                getworkCellLY(workCellFlag)
            }
        }
    }
    function getworkCellLY(workCellFlag) {
        console.log("abc")
        //取工单号
        var workOrderCode = $("#workOrderCode").val();
        if (workOrderCode == null || workOrderCode == "" || workOrderCode == undefined) {
            api.toast({
                msg: "请先扫描/录入工单号！",
                duration: 3000
            });
            return;
        }

        var tm = $("#tm").val();
        var type = $("input[name='type']:checked").val();
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "out_kj_sl",
                name: "uwmWorkOrderSL",
                relWorkCellGid: "", //目标库位
                tm: tm,
                workCellFlag: workCellFlag,
                operateType: type, //操作类型
                workOrderCode: workOrderCode, //工单号
                workCenterGid: ""
            }
        });
    }
    function getworkCellMB(workCellFlag) {
        console.log("123")
        //取工单号
        var workOrderCode = $("#workOrderCode").val();
        if (workOrderCode == null || workOrderCode == "" || workOrderCode == undefined) {
            api.toast({
                msg: "请先扫描/录入工单号！",
                duration: 3000
            });
            return;
        }

        var tm = $("#tm").val();
        var type = $("input[name='type']:checked").val();
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "out_kj_sl",
                name: "uwmWorkOrderSL",
                relWorkCellGid: relWorkCellGid, //目标库位
                tm: tm,
                workCellFlag: workCellFlag,
                operateType: type, //操作类型
                workOrderCode: workOrderCode, //工单号
                workCenterGid: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name, workCellFlag) {
        //源库位
        if (workCellFlag == "1") {

            workCellGid = id;
            workCellCode = code;
            workCellName = name;
            document.getElementById('workCellCode').value = code + ":" + name;

            //目标库位
        } else if (workCellFlag == "2") {

            relWorkCellGid = id;
            relWorkCellCode = code;
            relWorkCellName = name;
            document.getElementById('relWorkCellCode').value = code + ":" + name;
            $("#workCellCode").focus();
            var e = "workCellCode";
            idSelector = e;
        }
    }

    //9；PDA自带扫描回车事件-源库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#workCellCode").val(sm);
            getWorkCell(sm, "workCellCode", "1");
        }
    });

    //10；PDA自带扫描回车事件-目标库位
    $("#relWorkCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {

            $("#relWorkCellCode").val(sm);
            getWorkCell(sm, "relWorkCellCode", "2");
        }
    });

    //保存按钮
    function save() {
        //取工单号
        var workOrderCode = $("#workOrderCode").val();
        //物料号
        var mrlCode = $("#mrlCode").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //转移数量数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //箱号
        // var boxnum = $("#boxnum").val();
        var boxnum = boxnumE;
        //操作类型
        var type = $("input[name='type']:checked").val();
        //条码唯一号ID
        var tmId = $("#tmId").val();

        //工单号非空校验
        if (workOrderCode == null || workOrderCode == "" || workOrderCode == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：请录入工单号";
            return;
        }

        //目标库位非空校验
        if (relWorkCellGid == "" || relWorkCellGid == null || relWorkCellGid == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：请选择/扫描 目标库位";
            return;
        }
        //源库位校验
        console.log("保存时：" + workCellGid);
        if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：请选择/扫描 源库位";
            return;
        }

        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：箱号不能为空";
            return;
        }

        //物料非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：物料号不能为空";
            return;
        }

        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：批次号不能为空";
            return;
        }

        //转移数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：请输入转移数量";
            return;
        } else {
            if (parseFloat(qty) <= 0) {
                document.getElementById('infoDiv').style.background = "#ff3a3a";
                document.getElementById("resultInfo").innerHTML = "提示信息：转移数量必须大于0";
                return;
            }
        }

        //源仓库和目标仓库不能一样
        if (workCellGid != null && workCellGid != "" && workCellGid == relWorkCellGid) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：源库位和目标库位不能一样";
            return;
        }

        //操作类型非空校验
        if (type == null || type == "" || type == undefined) {
            document.getElementById('infoDiv').style.background = "#ff3a3a";
            document.getElementById("resultInfo").innerHTML = "提示信息：请选择操作类型";
            return;
        }

        //退料，需要选择源库位
        if (type == "OUT") {
            if (workCellGid == null || workCellGid == "" || workCellGid == undefined) {
                document.getElementById('infoDiv').style.background = "#ff3a3a";
                document.getElementById("resultInfo").innerHTML = "提示信息：退料需要选择源库位";
                return;
            }
        }

        //物料号和条码特殊字符处理
        api.showProgress({
            title: '保存中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmWorkOrderSlSave";
        var params = new Object();
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workOrderCode: workOrderCode, //工单号
                relWorkCellGid: relWorkCellGid, // 目标库位
                relWorkCellCode: relWorkCellCode, // 目标库位
                workCellGid: workCellGid, //源库位编码
                mrlCode: mrlCode, //物料号
                lotCode: lotCode, //批次号
                appVersion: api.appVersion,
                qty: qty, //转移数量
                tm: tm, //条码
                boxnum: boxnum, //箱号
                type: type, //操作类型
                tmId: tmId, //条码为一号ID
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    document.getElementById('infoDiv').style.background = "#66f10b";
                    document.getElementById("resultInfo").innerHTML = "提示信息：保存成功";
                    if (type == "IN") {
                        $("#workCellCode").val("");
                    }

                    //清空表单
                    $("#tm").val("");
                    $("#tmId").val("");
                    $("#boxnum").val("");
                    boxnumE = "";
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    //刷新表格数据
                    doSearch();

                } else if (myobj.code == '1') {
                    api.hideProgress();
                    //清空表单
                    if (type == "IN") {
                        $("#workCellCode").val("");
                    }
                    $("#tm").val("");
                    $("#tmId").val("");
                    $("#boxnum").val("");
                    boxnumE = "";
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    $("#boxnum").focus();
                    idSelector = "boxnum";
                    document.getElementById('infoDiv').style.background = "#ff3a3a";
                    document.getElementById("resultInfo").innerHTML = "提示信息：" + myobj.message;
        
                    $("#boxnum").focus();
                }
            } else {
                $("#boxnum").val("");
                boxnumE = "";
                $("#boxnum").select();
                idSelector = "boxnum";
                api.hideProgress();
                alert("连接超时，请耐心等待");
            }
        });
    }

    //自动保存
    function saveAuto() {
        //工单号
        var workOrderCode = $("#workOrderCode").val();

        //物料号
        var mrlCode = $("#mrlCode").val();
        //批次号
        var lotCode = $("#lotCode").val();
        //转移数量数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //箱号
        //var boxnum = $("#boxnum").val();
        var boxnum = boxnumE;
        //操作类型
        var type = $("input[name='type']:checked").val();

        //工单号非空校验
        if (workOrderCode == "" || workOrderCode == null || workOrderCode == undefined) {
            return;
        }
        //目标库位非空校验
        if (relWorkCellGid == "" || relWorkCellGid == null || relWorkCellGid == undefined) {
            return;
        }
        //源库位校验
        if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
            return;
        }
        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            return;
        }

        //物料非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            return;
        }

        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            return;
        }

        //转移数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            return;
        } else {
            if (parseFloat(qty) <= 0) {
                return;
            }
        }

        //源仓库和目标仓库不能一样
        if (workCellGid != null && workCellGid == relWorkCellGid) {
            return;
        }

        //操作类型非空校验
        if (type == null || type == "" || type == undefined) {
            return;
        }

        //退料需要选择源库位
        if (type == "OUT") {
            if (workCellGid == null || workCellGid == "" || workCellGid == undefined) {
                return;
            }
        }
        //保存
        save();
    }

    //扫描之后条码解析
    function tmSplit(sm) {
        var arr = sm.split(",");
        $("#tm").val(sm);
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            $("#mrlCode").val(arr[1]);
            document.getElementById("mrlCodetital").innerHTML = "物料编码：" + arr[1];
        }
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            let parsedNumber = parseFloat(arr[2]);

            // 将浮点数再转换为字符串，以防止科学计数法
            let resultString = parsedNumber.toString();
            $("#qty").val(resultString);
            document.getElementById("qtytital").innerHTML = "数量：" + resultString;
        }
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            $("#lotCode").val(arr[3]);
            document.getElementById("lotCodetital").innerHTML = "批次号：" + arr[3];
        }
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmId").val("");
        } else {
            $("#tmId").val(arr[4]);
        }
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
            boxnumE = "";
        } else {
            //$("#boxnum").val(arr[9]);
            boxnumE = arr[9];
            document.getElementById("boxnumtital").innerHTML = "箱号：" + arr[9];
        }

        getWorkCellBytM(sm, arr[9])


    }

    //根据箱码获取来源库位
    function getWorkCellBytM(tm, boxnum) {
        //操作类型
        var type = $("input[name='type']:checked").val();

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCellBytM";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                tm: tm,
                boxnum: boxnum,
                appVersion: api.appVersion,
                operationType: type,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.type == "0") {

                    //返回消息
                    if (myobj.id != "" && myobj.id != null && myobj.id != undefined) {
                        workCellGid = myobj.id;
                    }

                    workCellCode = myobj.code;

                    idSelector = "boxnum";
                    if (myobj.type == '1') {
                        $("#boxnum").val("");
                        boxnumE = "";
                        $("#boxnum").focus();
                        document.getElementById('infoDiv').style.background="#ff3a3a"; 
                        document.getElementById("resultInfo").innerHTML ="提示信息：" + myobj.message;
                    } else if(type == 'OUT') {
                        //console.log("是否走了这里" );
                        $("#relWorkCellCode").focus();
                        idSelector = "relWorkCellCode";
                        saveAuto();
                    } else {
                        $('#workCellCode').val(myobj.code + ":" + myobj.name);
                        //自动保存
                        saveAuto();
                    }

                    //失败
                } else if (myobj.type == "1") {
                    $("#boxnum").val("");
                    boxnumE = "";
                    $("#boxnum").focus();
                    document.getElementById('infoDiv').style.background="#ff3a3a"; 
                    document.getElementById("resultInfo").innerHTML ="提示信息：" + myobj.message;
                }

            }
        });
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            // tmSplit(tm);
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm)
                $("#boxnum").blur();
                //调用条码解析+保存事件
                tmSplit(tm);
            }
        }
    });

    //工单号回车事件
    $("#workOrderCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#workOrderCode").val(tm);

            //值改变
            onChange();
        }
    });

    function setWo(code) {
        console.log(code)
        $("#workOrderCode").val(code);
        //值改变
        onChange();
    }

    function goSelectWO() {
        //操作类型
        var type = $("input[name='type']:checked").val();

        api.openWin({
            name: 'selWorkOrderListNew',
            url: 'selWorkOrderListNew.html',
            pageParam: {
                name: "uwmWorkOrderSL",
                type: type,
            }
        });

        $("#boxnum").focus();
        idSelector = "boxnum";
    }

    //工单号值改变,查询表格数据
    function doSearch() {
        //工单号
        var workOrderCode = $("#workOrderCode").val();

        //工单号不空，查询上料信息
        if (workOrderCode == "" || workOrderCode == null || workOrderCode == undefined) {

            return;
        }


        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmWorkOrderSLSearch";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                appVersion: api.appVersion,
                workOrderCode: workOrderCode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var list = myobj.list;
                    var html = "";
                    console.log('查询结果：',JSON.stringify(list));

                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 160px;padding: 0.2rem 0.2rem;font-size: 0.7rem;' onclick='getMrlStock(\"" + list[i].mrlCode + "\",\"" + list[i].mrlName + "\")'>";
                        html += "<table style='width:100%;'>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料编码：" + list[i].mrlCode + "</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料名称：" + list[i].mrlName + "</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:50%;'>上料数：" + list[i].slQty + "/" + list[i].yslqty + "</td><td style='width:50%;'>扣料数：" + (list[i].klQty == null ? "" : list[i].klQty) + "</td></tr> ";
                        html += "<tr style='width:100%;'><td style='width:50%;'>上料端库存数：" + list[i].stockQty + "</td><td style='width:50%;'>配送类型：" + (list[i].distributeType == null ? "" : list[i].distributeType) + "</td></tr> ";
                        html += "<tr style='width:100%;'><td style='width:50%;'>端库存数：" + list[i].dstockQty + "</td><td style='width:50%;'>线边库存数：" + (list[i].xbkcQty == null ? "" : list[i].xbkcQty) + "</td></tr> ";
                        html += "<tr style='width:100%;'><td style='width:50%;'>报废数：" + (list[i].bfQty == null ? "" : list[i].bfQty) + "</td></tr> ";
                        html += "</table></div>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);

                } else if (myobj.code == '1') {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //工单号值改变  查询源库位或者目标库位 根据操作类型
    function doSearchWorkCell() {
        $('#relWorkCellCode').val("");
        $('#workCellCode').val("");
        //工单号
        var workOrderCode = $("#workOrderCode").val();
        //操作类型
        var type = $("input[name='type']:checked").val();

        //工单号不空，查询上料信息
        if (workOrderCode == "" || workOrderCode == null || workOrderCode == undefined) {

            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmWorkOrderSLSearchWorkCell";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                workOrderCode: workOrderCode,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 60,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回消息
                console.log("返回消息：" + myobj.message);
                console.log(myobj.code);
                console.log(myobj.workCenterCode);
                if (myobj.code == "0") {

                    //上料，目标库位赋值
                    if (type == "IN") {

                        relWorkCellGid = myobj.id;
                        relWorkCellCode = myobj.workCenterCode;
                        relWorkCellName = myobj.name;
                        workCenterGid = myobj.workCenterId;
                        if (myobj.workCenterCode != "" && myobj.workCenterCode != null && myobj.workCenterCode != undefined) {
                            $('#relWorkCellCode').val(myobj.workCenterCode + ":" + myobj.name);
                        }
                        //退料，源库位赋值
                    } else if (type == "OUT") {
                        
                        workCellGid = myobj.id;
                        workCellCode = myobj.workCenterCode;
                        workCellName = myobj.name;
                        workCenterGid = myobj.workCenterId;
                        if (myobj.workCenterCode != "" && myobj.workCenterCode != null && myobj.workCenterCode != undefined) {
                            $('#workCellCode').val(myobj.workCenterCode + ":" + myobj.name);
                        }

                    }

                    api.setFocus({
                        inputId: 'boxnum',
                        focus: true,
                    });
                    idSelector = "boxnum";
                    $('#produName').val(myobj.produName);
                    //失败
                } else if (myobj.code == "1") {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    
    function getMrlStock(mrlCode,mrlName) {

        api.openWin({
            name: 'uwmWorkOrderSL_getMrlStock',
            url: 'uwmWorkOrderSL_getMrlStock.html',
            pageParam: {
                name: "uwmWorkOrderSL",
                mrlCode: mrlCode,
                mrlName: mrlName,
            }
        });

    }
    
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }

    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };
</script>

</html>